﻿using NLog;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using WTAppletProjectHTTPS.Models;

namespace WTAppletProjectHTTPS.Controllers
{
    public class GetTotalController : ApiController
    {
        private static readonly Logger Logger = NLog.LogManager.GetCurrentClassLogger();
        public class EnterParameter
        {
            public string FormType { get; set; }
        }
        public class BackNum
        {
            public int TotalNum { get; set; }
        }
        // POST api/<controller>
        public async Task<HttpResponseMessage> Post(EnterParameter EP)
        {
            BackNum BN = new BackNum();
            ReturnObjectQuery RO = new ReturnObjectQuery();
            try
            {
                using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionName"].ConnectionString))
                {
                    await connection.OpenAsync();
                    var query = "";
                    if (EP.FormType == "F1")
                    {
                        query = $@"select sum(T.TotalCount) TotalCount from (
select top 1 COUNT(PADID) OVER() AS TotalCount 
from ProductAssociationDate
union all 
select top 1 COUNT(TSCMCID) OVER() AS TotalCount 
from TestStationChangeMaterialConfirmation
union all 
select top 1 COUNT(FSCMCID) OVER() AS TotalCount 
from FrameStationChangeMaterialConfirmation
union all 
select top 1 COUNT(LMSSCID) OVER() AS TotalCount 
from LaserMarkingStationSelfInspectionConfirmation
union all 
select top 1 COUNT(PSCICID) OVER() AS TotalCount 
from PackagingStationChangeInformationConfirmation
union all 
select top 1 COUNT(CPFIRID) OVER() AS TotalCount 
from ColaProductionFullInspectionRecord
union all 
select top 1 COUNT(CFPID) OVER() AS TotalCount 
from ColaFirstPiecePatrolInspectionRecord
union all 
select top 1 COUNT(CFIRID) OVER() AS TotalCount 
from ColaFinalInspectionRecord
union all 
select top 1 COUNT(CPFTID) OVER() AS TotalCount 
from ColaProductFunctionTest
union all 
select top 1 COUNT(CPFIID) OVER() AS TotalCount 
from ColaPackingFinalInspection
union all 
select top 1 COUNT(QCSID) OVER() AS TotalCount 
from QCSamplingFinishedFrame where QCSFormType = 'TS Cola生产自检' 
union all 
select top 1 COUNT(FFCID) OVER() AS TotalCount 
from A1A2EmployeeFrameFeedingConfirmation where FFCFormType = 'TS Cola生产自检'
) T";
                    }
                    else if (EP.FormType == "F2")
                    {
                        query = $@"select sum(T.TotalCount) TotalCount from (
select top 1 COUNT(QCSID) OVER() AS TotalCount 
from QCSamplingFinishedFrame where QCSFormType = 'TS A1线 面盖物料确认' 
union all 
select top 1 COUNT(FFCID) OVER() AS TotalCount 
from A1A2EmployeeFrameFeedingConfirmation where FFCFormType = 'TS A1线 面盖物料确认' 
) T";
                    }
                    using (var command = new SqlCommand(query, connection))
                    {
                        using (var reader = await command.ExecuteReaderAsync())
                        {
                            if (!reader.HasRows)
                            {
                                BN.TotalNum = 0;
                                Logger.Warn("未找到记录");
                                RO.Status = HttpStatusCode.OK;
                                RO.Message = "未找到记录";
                                RO.Data = BN;
                                return Request.CreateResponse(HttpStatusCode.OK, RO);
                            }
                            while (await reader.ReadAsync())
                            {
                                BN.TotalNum = reader.GetInt32(reader.GetOrdinal("TotalCount"));
                            }
                            Logger.Info("查询成功");
                            RO.Status = HttpStatusCode.OK;
                            RO.Message = "OK";
                            RO.Data = BN;
                            RO.TotalCount = 0;
                            return Request.CreateResponse(HttpStatusCode.OK, RO);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error("查询失败: " + ex.Message);
                RO.Status = HttpStatusCode.InternalServerError;
                RO.Message = "Error: " + ex.Message;
                RO.Data = null;
                return Request.CreateResponse(HttpStatusCode.InternalServerError, RO);
            }
        }
    }
}